#include"thread_pool.h"
#include <asm-generic/socket.h>
#include <mysql/mysql.h>
#include <sys/select.h>
#include "user_check.h"
#include "datebase.h"
void get_setting(char* setting,char* passwd){
    int i,j;
    for(i=0,j=0;passwd[i]&&j!=4;++i){
        if(passwd[i]=='$') ++j;
    }
    strncpy(setting,passwd,i);
}
void loginCheck1(user_info_t* user,MYSQL_STMT* stmt,MYSQL pconn){
    printf("logincheck1 \n");
    train_t t;
    memset(&t,0,sizeof(t));

    if(!select_username(user->name,stmt,pconn)){
        char str[100];
        select_cryptpasswd(user->name,stmt,pconn,str);
        strcpy(user->encrypted,str);
        char set[13]={0};
        get_setting(set,str);
        set[12]='\0';
        t.len=strlen(set);
        t.type=TASK_LOGIN_SECTION1_RESP_OK;
        strcpy(t.buff,set);
        sendn(user->sockfd,&t,8+t.len);
    }
    else{
        t.type=TASK_LOGIN_SECTION1_RESP_ERROR;
        sendn(user->sockfd,&t,8);
    }
    return;


}
void loginCheck2(user_info_t* user,const char* encrypted,MYSQL_STMT* stmt,MYSQL pconn){
    train_t t;
    memset(&t,0,sizeof(t));

    printf("user->encrypt=%s\n",user->encrypted);
    if(strcmp(user->encrypted,encrypted)==0){
        printf("验证通过！\n");
        user->status=STATUS_LOGIN;
        t.type=TASK_LOGIN_SECTION2_RESP_OK;
        user->user_id=select_userid(user->name,stmt,pconn);
        user->pwd= select_rootpwd(user->user_id,stmt,pconn);

        sendn(user->sockfd,&t,8);
    }
    else{
        printf("验证失败！\n");
        t.type=TASK_LOGIN_SECTION2_RESP_ERROR;
        sendn(user->sockfd,&t,8);
    }
    return;
}

